// pages/baiscInfo/basicInfo.js
var app = getApp();
const getdata = require("../../tool/GetDate.js")
var mobile = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '获取验证码', //获取手机验证码按钮文字
    userInfo: {},
    mobile: "",
    verificationCode: "",
    inputVerificationCode: "",
    name: "",
    number: "",
    verifyMessage: "",
    snsMsgWait: 60,
    color: "#1296db",
    smsFlag: false,
    inputInvitationCode: "",
    districtTitleArray: [],
    districtArray: [],
    districtIndex: 0,
    districtName: "",
    companyInfo:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: app.globalData.userInfo,
    });
  },

  onDistrictPickerChange: function (e) {
    this.setData({
      districtName: this.data.districtArray[e.detail.value].name,
      districtID: this.data.districtArray[e.detail.value].id,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //用户输入手机号码
  handleInputPhone: function (e) {
    this.setData({
      mobile: e.detail.value,
    });
    console.log(e.detail.value);
  },

  //用户输入的验证码
  handleVerificationCode: function (e) {
    this.setData({
      inputVerificationCode: e.detail.value,
    });
  },

  //输入邀请码
  handleInvitationCode: function (e) {
    this.setData({
      inputInvitationCode: e.detail.value,
    });
    console.log(e.detail.value);
  },

  //用户输入工号
  handleInputNumber: function (e) {
    this.setData({
      number: e.detail.value,
    });
    console.log(e.detail.value);
  },

  //用户输入姓名
  handleInputName: function (e) {
    this.setData({
      name: e.detail.value,
    });
    console.log(e.detail.value);
  },

  //获取到的验证码
  doGetCode: function () {
    let that = this;
    let message = "";
    let verify = true;
    // if (!that.data.districtName) {
    //   message += '区域不能为空\n';
    //   verify = false;
    // }
    if (!that.data.mobile) {
      message += '手机号码不能为空\n';
      verify = false;
    }

    if (verify) {
      var inter = setInterval(function () {
        this.setData({
          smsFlag: true,
          color: "#cccccc",
          text: this.data.snsMsgWait + 's后重发',
          snsMsgWait: this.data.snsMsgWait - 1
        });
        if (this.data.snsMsgWait < 0) {
          clearInterval(inter)
          this.setData({
            text: '获取验证码',
            color: "#1296db",
            snsMsgWait: 60,
            smsFlag: false
          });
        }
      }.bind(this), 1000);

      if (!that.data.smsFlag) {
        getdata.getdata({
          url: "gbt/UserVerify/SmsVerify/",
          data: that.data.mobile,
          success: function (res) {
            console.log(res.root);
            if (res.root) {
              that.setData({
                verificationCode: res.root.code,
                verifyMessage: res.root.message,
                // inputVerificationCode: res.root.code,
              });

              mobile = that.data.mobile;
            }
          }
        });
      }
    }
    else{
      that.setData({
        verifyMessage: message,
      });
    }
  },

  isEmptyVerify: function () {
    let that = this;
    let message = "";
    let verify = true;
    // if (!that.data.districtName) {
    //   message += '区域不能为空\n';
    //   verify = false;
    // }
    if (!that.data.mobile) {
      message += '手机号码不能为空\n';
      verify = false;
    }
    console.log(that.data.mobile);
    console.log(mobile);
    if (that.data.mobile != mobile) {
      message += '手机号码错误，请确认\n';
      verify = false;
    }
    if (this.data.verificationCode === "") {
      message += '请获取验证码\n';
      verify = false;
    }
    if (this.data.inputVerificationCode === "") {
      message += '验证码不能为空\n';
      verify = false;
    }
    if (!that.data.inputInvitationCode) {
      message += '邀请码不能为空\n';
      verify = false;
    }
    if (!that.data.name) {
      message += '姓名不能为空\n';
      verify = false;
    }
    if (that.data.verificationCode != that.data.inputVerificationCode) {
      message += '验证码错误，请重新输入或重新获取\n';
      verify = false;
    }
    that.setData({
      verifyMessage: message,
    });
    return verify;
  },

  submit: function () {
    wx.showLoading({
      title: '正在注册···',
    });
    if (this.isEmptyVerify()) {
      let that = this;
      let message = "";
      let verify = true;

      let param = {};
      param.code = that.data.inputInvitationCode;
      getdata.postdata({
        url: "gbt/UserVerify/InvitationCodeVerify/",
        data: JSON.stringify(param),
        success: function (res) {
          console.log(res.root);
          if (res.root) {
            if (!res.root.state) {
              message += res.root.message + "\n";
              verify = false;
            }
            let districtID = res.root.data[0].districtID;
            console.log(res.root.data[0].districtID);
            // getdata.getdata({
            //   url: "gbt/UserVerify/MobileVerify/" + districtID + "/",
            //   data: that.data.mobile,
            //   success: function(res) {
            //     console.log(res.root);
            //     if (res.root) {
            //       if (!res.root.state) {
            //         message += res.root.message + "\n";
            //         verify = false;
            //       }

            //       that.setData({
            //         verifyMessage: message,
            //       });

            if (verify) {
              let jParams = {};
              jParams.mobile = that.data.mobile;
              jParams.number = that.data.number;
              jParams.name = that.data.name;
              jParams.nickName = that.data.userInfo.nickName;
              jParams.country = that.data.userInfo.country;
              jParams.province = that.data.userInfo.province;
              jParams.city = that.data.userInfo.city;
              jParams.language = that.data.userInfo.language;
              jParams.districtID = districtID;

              wx.login({
                success(res) {
                  if (res.code) {
                    //发起网络请求
                    jParams.jscode = res.code;
                    console.log(jParams);

                    getdata.postdata({
                      url: "gbt/UserVerify/SetUser",
                      data: JSON.stringify(jParams),
                      success: function (res) {
                        console.log(res.root);
                        if (res.root) {
                          wx.setStorage({
                            key: 'myUserData',
                            data: res.root.userInfo.data[0],
                          })
                          app.globalData.myUserData = res.root.userInfo.data[0];
                          wx.showToast({
                            title: `${res.root.message}`,
                            icon: 'none',
                            success: function (res) {
                              setTimeout(function () {
                                wx.switchTab({
                                  url: '/pages/index/index',
                                });

                                wx.hideLoading();
                              }, 2000);
                            }
                          })
                        } //返回结果数据判断的末尾（注册用户）
                      }
                    });

                  } else {
                    wx.hideLoading();
                    console.log('注册失败！' + res.errMsg)
                  }
                }
              })
              //获取微信登录的jscode
            } else {
              wx.hideLoading();
            }
            // }
            // else
            // {
            //   wx.hideLoading();
            // }
            //返回结果数据判断的末尾（邀请码）
            //   }
            // });
            //手机号验证末尾
          } //返回结果数据判断的末尾（邀请码）
          else {
            wx.hideLoading();
          }
        }
      });
      //邀请码验证末尾
    } else {
      wx.hideLoading();
    }
  },
  //公司入驻
  onCompany:function(){

  },
})